package com.quan.weixin.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.common.core.MyBaseServiceImpl;
import com.quan.weixin.entity.WeixinMenu;
import com.quan.weixin.mapper.WeixinMenuMapper;
import com.quan.weixin.service.WeixinMenuService;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;

/**
 * <p>
 * 微信菜单 服务实现类
 * </p>
 *
 * @author 杨浩泉
 * @since 2019-11-12
 */
@Slf4j
@Service
public class WeixinMenuServiceImpl extends MyBaseServiceImpl<WeixinMenuMapper, WeixinMenu> implements WeixinMenuService {

	@Autowired
	private WxMpService wxMpService ;
	
	@Override
	public boolean removeById(Serializable id) {
		return this.recursiveDelete(id);
	}
	
	private boolean recursiveDelete(Serializable id) {
		QueryWrapper<WeixinMenu> queryWrapper = new QueryWrapper<WeixinMenu>();
		queryWrapper.eq("pid", id);
		
		List<WeixinMenu> child = super.list(queryWrapper) ;
		if(null != child) {
			for (WeixinMenu entity : child) {
				recursiveDelete(entity.getId());
			}
		}
		return super.removeById(id);
	}

	@Override
	public boolean pushMenuToServer() {
		WxMenu menu = new WxMenu();
		
		QueryWrapper<WeixinMenu> queryWrapper = new QueryWrapper<WeixinMenu>();
		queryWrapper.eq("pid", 0);
		
		List<WeixinMenu> rootList = super.list(queryWrapper);
		rootList.forEach(item -> {
			WxMenuButton button = new WxMenuButton();
			button.setName(item.getName());
			button.setKey(item.getKeyValue());
			button.setMediaId(item.getMediaId());
			button.setPagePath(item.getPagepath());
			button.setType(item.getType());
			button.setUrl(item.getUrl());
			
			recursiveWxMenu(item, button);
			
			menu.getButtons().add(button) ;
		});
		
		try {
			this.wxMpService.getMenuService().menuCreate(menu);
			System.out.println(this.wxMpService.getAccessToken());
			log.info("推送菜单到微信服务器：{}", JSON.toJSONString(menu));
			return true;
		} catch (WxErrorException e) {
			log.error(e.getMessage());
			return false;
		}
	}
	
	private void recursiveWxMenu(WeixinMenu menu, WxMenuButton button) {
		
		QueryWrapper<WeixinMenu> queryWrapper = new QueryWrapper<WeixinMenu>();
		queryWrapper.eq("pid", menu.getId());
		List<WeixinMenu> childs = super.list(queryWrapper);
		
		if (null != childs && childs.size() > 0) {
			for (WeixinMenu child : childs) {
				if(child.getStatus()) {
					WxMenuButton childButton = new WxMenuButton();
					childButton.setName(child.getName());
					childButton.setKey(child.getKeyValue());
					childButton.setMediaId(child.getMediaId());
					childButton.setPagePath(child.getPagepath());
					childButton.setType(child.getType());
					childButton.setUrl(child.getUrl());
					
					button.getSubButtons().add(childButton);
					
					recursiveWxMenu(child, childButton);
				}
			}
		}
	}

}
